<template>
  <div class="settings-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column property="name" label="角色名称" width="240" />
              <el-table-column property="description" label="描述" />
              <el-table-column label="操作">
                <!-- <template slot-scope="scope"> -->
                <!-- <template v-slot="scope"> -->
                <template #default="scope">
                  <el-button size="small" type="success" @click="hAssignPermission(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDelRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hReset"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="hReset">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹窗 -->
      <el-dialog v-if="showPermission" :visible.sync="showPermission" title="分配权限">
        <assignPermission :role-id="curRoleId" @close="hClose" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, delRoles, addRoles, editRoles } from '@/api/settings'
import assignPermission from '@/views/settings/assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      pageParams: {
        page: 1,
        pagesize: 2
      },
      roleList: [],
      total: 0,
      roleForm: {
        name: '',
        description: ''
      }, rules: {
        name: [
          { required: true, message: '输入角色名', taggiter: 'blur' }
        ],
        description: []
      },
      showDialog: false,
      isEdit: false, // flase是新增，true是编辑
      showPermission: false,
      curRoleId: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 弹出分配权限弹窗
    hAssignPermission(id) {
      this.showPermission = true
      this.curRoleId = id
    },
    hClose() {
      this.showPermission = false
    },
    // 获取角色列表
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res.data.rows, 'getRoles')
        this.roleList = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.page = 1
      this.pageParams.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      this.loadRoles()
    },
    // 删除角色
    async doDel(id) {
      try {
        const res = await delRoles(id)
        console.log(res, '删除成功')
        // page=2 删除最后一条数据
        if (this.roleList.length === 1) {
          console.log('我是111111')
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
      } catch (error) {
        console.log(error)
      }
    },
    hDelRoles(id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {})
    },
    // 新增角色
    async hAddRoles() {
      try {
        const res = await addRoles(this.roleForm)
        console.log(res, '添加')
        this.showDialog = false
        this.total++
        // 新增跳转到最大页码
        const maxPage = Math.ceil(this.total / this.pageParams.pagesize)
        this.pageParams.page = maxPage
        this.loadRoles()
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    hEdit(obj) {
      this.showDialog = true
      this.isEdit = true
      // 将传来的数据填充到表单
      this.roleForm = { ...obj }
    },
    async  doEdit() {
      try {
        const res = await editRoles(this.roleForm)
        console.log(res, 'editRoles')
        this.showDialog = false
        this.loadRoles()
      } catch (error) {
        console.log(error)
      }
    },
    // 提交
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.hAddRoles()
      }
      )
    },
    // 重置
    hReset() {
      this.$refs.roleForm.resetFields()
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style>

</style>
